﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Threading;

using Microsoft.Devices;

namespace Coding4Fun.FakeCall.Controls
{
	public partial class ReceivingCall : CallingUserControl
	{
		DispatcherTimer _vibrateTimer;
		DispatcherTimer _ringTimer;
		double _lockHeight;
		public event RoutedEventHandler CallAnswered;
		bool handledLock = false;

		public ReceivingCall()
		{
			InitializeComponent();
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			StopAllCallActions();
			EndCall(e);
		}

		private void Button_Click_1(object sender, RoutedEventArgs e)
		{
			StopAllCallActions();
			InvokeCallAnswered(e);
		}

		private void CallingUserControl_Unloaded(object sender, RoutedEventArgs e)
		{
			StopAllCallActions();
		}

		private void InvokeCallAnswered(RoutedEventArgs e)
		{
			StopAllCallActions();

			var handler = CallAnswered;

			if (handler != null)
				handler(this, e);
		}
		
		public new Visibility Visibility
		{
			get { return base.Visibility; }
			set
			{
				base.Visibility = value;

				if (value == Visibility.Visible)
				{
					StartTimers();
				}
				else
				{
					StopAllCallActions();
				}
			}
		}

		void _ringTimer_Tick(object sender, EventArgs e)
		{
			if (Visibility == Visibility.Collapsed)
				return;

			player.Position = new TimeSpan(0);
			player.Play();
		}

		void _vibrateTimer_Tick(object sender, EventArgs e)
		{
			if (Visibility == Visibility.Collapsed)
				return;

			VibrateController.Default.Start(new TimeSpan(0, 0, 0, 0, 500));
		}

		private void StartTimers()
		{
			if (Settings.VibratePhone)
			{
				_vibrateTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
				_vibrateTimer.Tick += _vibrateTimer_Tick;

				_vibrateTimer_Tick(null, null);
				_vibrateTimer.Start();
			}

			if (Settings.RingPhone)
			{
				_ringTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2.5) };
				_ringTimer.Tick += _ringTimer_Tick;

				_ringTimer_Tick(null, null);
				_ringTimer.Start();
			}
		}

		public void StopAllCallActions()
		{
			if(_vibrateTimer != null)
				_vibrateTimer.Stop();

			if (_ringTimer != null)
				_ringTimer.Stop();

			_vibrateTimer = null;
			_ringTimer = null;
		}

		private void manipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
			if (handledLock || gridTranslate.Y >= 0)
				return;

			CoastGrid.Stop();
			gridTranslate.Y = e.TotalManipulation.Translation.Y < _lockHeight ? _lockHeight : e.TotalManipulation.Translation.Y;

			IEasingFunction easing;

			if (e.IsInertial && e.FinalVelocities.LinearVelocity.Y < -1000 ||
				gridTranslate.Y <= _lockHeight)
			{
				handledLock = true;
				coastY.Value = _lockHeight;
				easing = new CircleEase();
				((CircleEase)easing).EasingMode = EasingMode.EaseIn;
				slideBar.Visibility = Visibility.Collapsed;
			}
			else
			{
				coastY.Value = 0;
				easing = new BounceEase();
				((BounceEase)easing).EasingMode = EasingMode.EaseOut;
				((BounceEase)easing).Bounces = 2;
				((BounceEase)easing).Bounciness = 5;
			}
			coastY.EasingFunction = easing;
			CoastGrid.Begin();
		}
		
		private void manipulationDelta(object sender, ManipulationDeltaEventArgs e)
		{
			e.Handled = true;
			if (handledLock)
				return;

			gridTranslate.Y += e.DeltaManipulation.Translation.Y;
			if (gridTranslate.Y > 0)
				gridTranslate.Y = 0;
			else if (gridTranslate.Y < _lockHeight)
			{
				gridTranslate.Y = _lockHeight;
				e.Complete();
			}
		}
		
		private void manipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			_lockHeight = -1 * (ActualHeight - receivingPerson.ActualHeight - phoneInfo.ActualHeight);
		}
	}
}